<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120">
                <template v-slot="scope">
                  <span>{{ (pageParams.page-1) * pageParams.pagesize + scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <!-- {{ scope.row }} -->
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2, 3, 5, 8]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <!-- 新增页面弹框 -->
    <el-dialog
      title="编辑弹层"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showDialog=false">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addRole, deleteRole, getRole, updateRole } from '@/api/roles'
export default {
  data() {
    return {
      list: [],
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0, // 总的数据条数
      showDialog: false,
      isEdit: false
    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    async loadRole() {
      const res = await getRole(this.pageParams)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange(size) {
      this.pageParams.pagesize = size
      this.loadRole()
    },
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.loadRole()
    },
    async hDel(id) {
      try {
        // 如果当前页面只剩下一条数据，并且页数大于1 删除后则让页面数自动减1
        if (this.list.length && this.pageParams.page > 1) {
          this.pageParams.page--
        }
        const result = await this.$confirm('您确定要删除吗？', '提示', { type: 'warning' }).catch(e => e)
        // if(!result) return
        // console.log(result)
        if (result !== 'confirm') return
        const res = await deleteRole(id)
        // console.log(res)
        this.$message.success(res.message)
        this.loadRole()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    async doAdd() {
      const res = await addRole(this.roleForm)
      // console.log(res)
      this.$message.success(res.message)
      this.showDialog = false
      // this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
      if (this.total % this.pageParams.pagesize === 0) {
        this.total++
      }
      this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
      this.loadRole()
    },
    hEdit(row) {
      this.showDialog = true
      this.isEdit = true
      this.roleForm = { ...row }
    },
    async doEdit() {
      const res = await updateRole(this.roleForm)
      this.$message.success(res.message)
      this.showDialog = false
      this.$refs.roleForm.resetFields()
    },
    async hSubmit() {
      const valid = await this.$refs.roleForm.validate().catch(e => e)
      if (!valid) return
      this.isEdit ? this.doEdit() : this.doAdd()
      // this.doEdit()
    },
    hClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>
